﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using LPV_POS.Module;
using DevExpress.XtraGrid.Views.Base;
using LPV_POS;
using System.Resources;
using System.Globalization;

namespace LPV_POS.mh_pos
{
    public partial class mh_dmdonvitinh : DevExpress.XtraEditors.XtraForm
    {
        CultureInfo culture;
        thuvien tv;
        DataTable tb;
        string[] s;
        object[] o;

        #region "Muti language"

        private void SetLanguage_banhang(string cultureName)
        {
            try
            {
                culture = CultureInfo.CreateSpecificCulture(cultureName);
                ResourceManager rm = new ResourceManager("LPV_POS.Muti_language.MyResource", typeof(mh_banhang6_PhuThu).Assembly);
                label2.Text = rm.GetString("ten", culture);
                label1.Text = rm.GetString("dvt", culture);
                bton_tim.Text = rm.GetString("tim", culture);
                bton_them.Text = rm.GetString("them", culture);
                bton_sua.Text = rm.GetString("sua", culture);
                bton_xoa.Text = rm.GetString("xoa", culture);
                bton_luu.Text = rm.GetString("luu", culture);
                bton_thoat.Text = rm.GetString("thoat", culture);
                simpleButton1.Text = rm.GetString("quydoidonvi", culture);
                label1.Text = rm.GetString("dvt", culture);
                label2.Text = rm.GetString("ten", culture);
                label1.Text = rm.GetString("dvt", culture);
                this.grid_donvitinh.GroupPanelText = rm.GetString("danhsachdonvitinh", culture);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Note");
            }

        }

        #endregion
        // select danh sach don vi tinh
        private DataTable f_selectdonvitinh()
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose" };
            o = new object[] { 1, 1 };
            tb = tv.SP_SelectDataTable("sp_mh_dmdonvitinh", s, o);
            DataView dv_temp = tb.DefaultView;
            dv_temp.Sort = "OBJID DESC";
            return dv_temp.ToTable();
        }
        // select danh sach don vi tinh co dieu kien
        private DataTable f_selectdonvitinhdk(string tendonvitinh)
        {
           
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJNAME" };
            o = new object[] { 1, 2,tendonvitinh };
            tb = tv.SP_SelectDataTable("sp_mh_dmdonvitinh", s, o);
            DataView dv_temp = tb.DefaultView;
            dv_temp.Sort = "OBJID DESC";
            return dv_temp.ToTable();
        }
        // insert mot don vi tinh
        private int f_insertdonvitinh(string tendonvitinh)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJNAME" };
            o = new object[] { 2, 1, tendonvitinh };
            int ketqua = tv.SP_Update("sp_mh_dmdonvitinh", s, o);
            return ketqua;
        }

        // update mot don vi tinh
        private int f_updatedonvitinh(string madonvitinh, string tendonvitinh)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJID", "@OBJNAME" };
            o = new object[] { 3, 1, madonvitinh, tendonvitinh };
            int ketqua = tv.SP_Update("sp_mh_dmdonvitinh", s, o);
            return ketqua;
        }

        // xoa don vi tinh
        private int f_deletedonvitinh(string madonvitinh)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJID" };
            o = new object[] { 4, 1, madonvitinh };
            int ketqua = tv.SP_Update("sp_mh_dmdonvitinh", s, o);
            return ketqua;
        }
       
        public mh_dmdonvitinh()
        {
            InitializeComponent();
            culture = CultureInfo.CurrentCulture;
        }

        private void mh_dmdonvitinh_Load(object sender, EventArgs e)
        {
            cpublic ocpublic = new cpublic();
            string str_lang = ocpublic.Registry_Get("mh_main_lang");
            if (str_lang == "")
                str_lang = "vi-VN";
            SetLanguage_banhang(str_lang);
            luoi_donvitinh.MainView = grid_donvitinh;
            luoi_donvitinh.DataSource = f_selectdonvitinh();

            bton_luu.Enabled = false;
            bton_tendonvitinh.Enabled = false;
        }

        private void grid_donvitinh_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            GridView grid = (GridView)sender;
            //   str_makho = grid.GetRowCellValue(grid_khohang.FocusedRowHandle, col_makho).ToString();
            int chiso = e.FocusedRowHandle;
            string _chiso = Convert.ToString(chiso);
            GridColumn col = grid_donvitinh.FocusedColumn;
            txt_tendonvitinh.Text = grid_donvitinh.GetRowCellDisplayText(chiso, col_tendonvitinh) + "";

            bton_luu.Enabled = false;
            bton_tendonvitinh.Enabled = false;
        }

        private void bton_them_Click(object sender, EventArgs e)
        {
            txt_tendonvitinh.Text = "";
            bton_luu.Enabled = true;
            bton_tendonvitinh.Enabled = true;
            Variable.str_trangthaithemsua = "them";

        }

        private void bton_sua_Click(object sender, EventArgs e)
        {

            bton_luu.Enabled = true;
            bton_tendonvitinh.Enabled = true;
            Variable.str_trangthaithemsua = "sua";

        }

        private void bton_xoa_Click(object sender, EventArgs e)
        {
           
            int ketqua;
            if (luoi_donvitinh.DefaultView.RowCount <= 0)    // neu khong co dong nao thi khong xoa duoc
                return;
            string madonvitinh = grid_donvitinh.GetRowCellValue(grid_donvitinh.FocusedRowHandle, col_madonvitinh).ToString();
            Mod_messagebox mess = new Mod_messagebox();
            mess.messagebox_pos("Thông báo", "Bạn muốn xóa?", loaithongbao.yesno.GetHashCode());
            if (Variable.thongbao == true)
            {
                ketqua = f_deletedonvitinh(madonvitinh);
                if (ketqua == 2)
                {
                    //Mod_messagebox mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Xóa không thành công", loaithongbao.canhbao.GetHashCode());
                    return;
                }
                // xoa mot dong trong luoi don vi tinh
                int chiso;
                chiso = ((ColumnView)luoi_donvitinh.Views[0]).FocusedRowHandle;
                DataView tam = (DataView)luoi_donvitinh.Views[0].DataSource;
                tam.Delete(chiso);
            }
        }

        private void bton_luu_Click(object sender, EventArgs e)
        {
            int ketqua;

            if (txt_tendonvitinh.Text == "")       // không thể luu neu chua nhap ten don vi tinh
                return;
            int chiso = grid_donvitinh.FocusedRowHandle;
            if (Variable.str_trangthaithemsua == "them")
            {
                ketqua = f_insertdonvitinh(txt_tendonvitinh.Text);

                if (ketqua == 2)
                {
                    Mod_messagebox mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Thêm không thành công", loaithongbao.canhbao.GetHashCode());
                    return;
                }
            }
            else if (Variable.str_trangthaithemsua == "sua")
            {
                string madonvitinh = grid_donvitinh.GetRowCellValue(grid_donvitinh.FocusedRowHandle, col_madonvitinh).ToString();
                ketqua = f_updatedonvitinh(madonvitinh, txt_tendonvitinh.Text);

                if (ketqua == 2)
                {
                    Mod_messagebox mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Chỉnh sửa không thành công", loaithongbao.canhbao.GetHashCode());
                    return;
                }
               // luoi_donvitinh.DataSource = f_selectdonvitinh();
            }
            luoi_donvitinh.DataSource = null;
            luoi_donvitinh.DataSource = f_selectdonvitinh();
            if (Variable.str_trangthaithemsua == "sua")
                grid_donvitinh.FocusedRowHandle = chiso;
        }

        private void bton_thoat_Click(object sender, EventArgs e)
        {
            Mod_messagebox mess = new Mod_messagebox();
            mess.messagebox_pos("Thông báo", "Bạn muốn thoát?", loaithongbao.yesno.GetHashCode());
            if (Variable.thongbao == true)
            {

                this.Close();
            }
        }

        private void bton_tendonvitinh_Click(object sender, EventArgs e)
        {
            string _dvt = new cpublic().loadformchu_point(300, 250);
            if (_dvt != "")
                txt_tendonvitinh.Text = _dvt;
        }

        private void bton_tim_Click(object sender, EventArgs e)
        {
            string tendvt = "";
            if (txt_timtendonvitinh.Text != "")
                tendvt = "'%" + txt_timtendonvitinh.Text + "%'";
            else
                tendvt = txt_timtendonvitinh.Text;
            luoi_donvitinh.DataSource = null;
            luoi_donvitinh.DataSource = f_selectdonvitinhdk(tendvt);
        }

        private void bton_nhaptimdvt_Click(object sender, EventArgs e)
        {
            txt_timtendonvitinh.Text = new cpublic().loadformchu_point(300, 250);
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            mh_doidvt omh_doidvt = new mh_doidvt();
            omh_doidvt.ShowDialog();
        }
    }
}